<!--
 * @description: 分析出来的角色列表
 * @fileName: RolesList
 * @author: 17076
 * @date: 2025/3/31-下午2:33
 * @version: V1.0.0
-->

<template>
  <cs-dialog
    :dialog="dialogOption"
    @onSubmit="save"
  >
    <div slot="content">
      <div style="text-align: right">
        <el-button icon="el-icon-circle-plus-outline" @click="handleAddRole">添加角色</el-button>
        <el-button @click="handleCreateRoles">重新生成</el-button>
      </div>
      <vxe-grid v-bind="gridOption" style="margin-top: 10px">
        <template #opera="{ row }">
          <el-button type="text" size="mini" icon="el-icon-delete" @click="deleteRoles(row)" />
        </template>
      </vxe-grid>
      <add-roles ref="add" :play-id="playId" @save="fetchData" />
    </div>
  </cs-dialog>
</template>

<script>
import mixin from '@/views/mixin'
import { deletePlayRoles, selectPlayRole } from '@/api/roles'
import AddRoles from './AddRoles.vue'
import { getPlayRole } from '@/api/play'
export default {
  name: 'RolesList',
  components: { AddRoles },
  mixins: [mixin],
  props: {
    playId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      dialogOption: {
        title: {
          title: '角色确认'
        },
        show: false
      },
      gridOption: {
        ...mixin.data().gridOptions,
        loading: false,
        columns: [
          { type: 'seq', width: 80 },
          { field: 'name', title: '角色名称' },
          { field: 'remark', title: '角色描述' },
          { title: '操作', slots: { default: 'opera' }, width: 50 }
        ],
        data: []
      }
    }
  },
  methods: {
    show() {
      this.dialogOption.show = true
      this.fetchData()
    },
    // 查询角色列表
    fetchData() {
      selectPlayRole({ playId: this.playId }).then(res => {
        this.gridOption.data = res.data.result
      })
    },
    // 大模型分析角色列表
    handleAnalysis() {
      this.gridOption.loading = true
      getPlayRole({ id: this.playId }).then(res => {
        this.roleList = res.data
        this.gridOption.loading = false
      }).catch(() => {
        this.gridOption.loading = false
      })
    },
    // 删除角色
    deleteRoles(row) {
      this.$baseConfirm('确定删除角色吗?', '提示', async() => {
        const { code, msg } = await deletePlayRoles({ id: row.id })
        code === 200 ? this.$baseMessage.success(msg || '删除成功') : this.$baseMessage.error(msg || '删除失败')
        this.fetchData()
      })
    },
    // 添加角色
    handleAddRole() {
      this.$refs.add.show()
    },
    // 重新生成角色
    handleCreateRoles() {
      this.$baseConfirm('确定重新生成角色吗?', '提示', async() => {
        this.handleAnalysis()
      })
    },
    save(callback) {
      callback(true)
    }
  }
}
</script>

<style scoped lang="scss">

</style>
